<!DOCTYPE html>
<html>
<head>
    <title>Bridge Rectifier Circuit</title>
</head>
<body>
    <canvas id="circuitCanvas" width="650" height="320"></canvas>
    <script>
        const canvas = document.getElementById('circuitCanvas');
        const ctx = canvas.getContext('2d');

        // Style settings
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        ctx.lineWidth = 2;
        ctx.font = '18px Arial';

        // --- Helper functions ---
        function drawLine(x1, y1, x2, y2) {
            ctx.beginPath();
            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);
            ctx.stroke();
        }

        function drawTerminal(x, y, radius) {
            ctx.beginPath();
            ctx.arc(x, y, radius, 0, 2 * Math.PI);
            ctx.save();
            ctx.fillStyle = 'white';
            ctx.fill();
            ctx.restore();
            ctx.stroke();
        }

        function drawConnectionDot(x, y, radius) {
            ctx.beginPath();
            ctx.arc(x, y, radius, 0, 2 * Math.PI);
            ctx.fill();
        }

        // --- Main Drawing Logic ---

        // Y coordinates for main horizontal wires
        const y_top = 100;
        const y_bottom = 200;
        const y_mid = (y_top + y_bottom) / 2;

        // 1. Input Section
        const input_label_x = 40;
        const input_term_x = 110;
        const rect_in_x = 180;

        ctx.textAlign = 'center';
        ctx.fillText('input', input_label_x, y_mid + 5);
        
        // Sine wave symbol, placed between terminals
        const sine_x = input_term_x;
        ctx.beginPath();
        ctx.moveTo(sine_x, 125);
        ctx.quadraticCurveTo(sine_x + 15, 140, sine_x, 150);
        ctx.quadraticCurveTo(sine_x - 15, 160, sine_x, 175);
        ctx.stroke();
        
        // Input terminals
        drawTerminal(input_term_x, y_top, 5);
        drawTerminal(input_term_x, y_bottom, 5);
        
        // Wires from terminals to rectifier
        drawLine(input_term_x, y_top, rect_in_x, y_top);
        drawLine(input_term_x, y_bottom, rect_in_x, y_bottom);

        // 2. Bridge Rectifier Section
        const rect_out_x = 480;
        const rect_y = 60;
        const rect_h = 180;
        
        // Dashed box
        ctx.beginPath();
        ctx.setLineDash([6, 4]);
        ctx.strokeRect(rect_in_x, rect_y, rect_out_x - rect_in_x, rect_h);
        ctx.setLineDash([]); // Reset to solid line

        // Labels inside box
        ctx.textAlign = 'center';
        ctx.fillText('bridge', (rect_in_x + rect_out_x) / 2, y_mid - 10);
        ctx.fillText('rectifier', (rect_in_x + rect_out_x) / 2, y_mid + 15);

        // Connection dots
        drawConnectionDot(rect_in_x, y_top, 4);
        drawConnectionDot(rect_in_x, y_bottom, 4);
        drawConnectionDot(rect_out_x, y_top, 4);
        drawConnectionDot(rect_out_x, y_bottom, 4);
        
        // 3. Output Section
        const res_start_x = 520;
        const res_width = 25;
        
        // Wires to resistor
        drawLine(rect_out_x, y_top, res_start_x, y_top);
        drawLine(rect_out_x, y_bottom, res_start_x, y_bottom);
        
        // Resistor
        ctx.beginPath();
        ctx.rect(res_start_x, y_top, res_width, y_bottom - y_top);
        ctx.stroke();

        // Label for resistor
        ctx.textAlign = 'left';
        ctx.fillText('output', res_start_x + res_width + 10, y_mid - 10);
        ctx.fillText('resistor R', res_start_x + res_width + 10, y_mid + 15);

        // 4. Figure Caption
        ctx.textAlign = 'center';
        ctx.font = '18px Arial';
        ctx.fillText('Fig. 10.1', (rect_in_x + rect_out_x) / 2, 290);
    </script>
</body>
</html>